rm(list=ls())

set.seed(1234)

library(plyr)
library(ggplot2)
library(car)
library(psych)
library(texreg)
library(effectsize)

setwd(".../Data/Raw")

Mex<-read.csv('Mexico.csv')

#subset model data to necessary covariates 

modeldata<-Mex[,c(6, 14:66, 68:73)]

modeldata<-subset(modeldata, Duration..in.seconds. > 350) #remove speedsters, those who completed the whole survey in less than 350 seconds


#Recode outcome variables --> higher values == fairer / stronger agreement 

modeldata$decide_citizen <-as.character(modeldata$decide_citizen)
modeldata$decide_women <-as.character(modeldata$decide_women)
modeldata$decide_animal <-as.character(modeldata$decide_animal)
modeldata$fair_women <-as.character(modeldata$fair_women)
modeldata$fair_animal <-as.character(modeldata$fair_animal)
modeldata$decisionprocess <-as.character(modeldata$decisionprocess)
modeldata$overturn <-as.character(modeldata$overturn)
modeldata$council_trust <-as.character(modeldata$council_trust)
modeldata$personalcouncil <-as.character(modeldata$personalcouncil)

modeldata[modeldata =="Muy injusto"]<-1
modeldata[modeldata =="Algo injusto"]<-2
modeldata[modeldata =="Algo  injusto"]<-2
modeldata[modeldata =="Algo justo"]<-3
modeldata[modeldata =="Muy justo"]<-4

modeldata[modeldata =="Muy injusta"]<-1
modeldata[modeldata =="Algo injusta"]<-2
modeldata[modeldata =="Algo justa"]<-3
modeldata[modeldata =="Muy justa"]<-4


modeldata[modeldata =="Muy en desacuerdo"]<-1
modeldata[modeldata =="Algo en desacuerdo"]<-2
modeldata[modeldata =="Algo de acuerdo"]<-3
modeldata[modeldata =="Muy de acuerdo"]<-4

modeldata$decide_citizen<-as.numeric(as.character(modeldata$decide_citizen))
modeldata$decide_women <-as.numeric(as.character(modeldata$decide_women))
modeldata$decide_animal <-as.numeric(as.character(modeldata$decide_animal))
modeldata$fair_women <-as.numeric(as.character(modeldata$fair_women))
modeldata$fair_animal <-as.numeric(as.character(modeldata$fair_animal))
modeldata$decisionprocess <-as.numeric(as.character(modeldata$decisionprocess))
modeldata$overturn <-as.numeric(as.character(modeldata$overturn))
modeldata$council_trust <-as.numeric(as.character(modeldata$council_trust))
modeldata$personalcouncil <-as.numeric(as.character(modeldata$personalcouncil))

#reverse code overturn, so higher values represent higher legitimacy views

modeldata$overturnR<-recode(modeldata$overturn, " 1=4; 2=3; 3=2; 4=1")


#create treatment variable

modeldata$Vignettes_DO_treat1 <-recode(modeldata$Vignettes_DO_treat1, " 1 = 1; else=0")
modeldata$Vignettes_DO_treat2 <-recode(modeldata$Vignettes_DO_treat2, " 1 = 2; else=0")
modeldata$Vignettes_DO_treat3 <-recode(modeldata$Vignettes_DO_treat3, " 1 = 3; else=0")
modeldata$Vignettes_DO_treat4 <-recode(modeldata$Vignettes_DO_treat4, " 1 = 4; else=0")
modeldata$Vignettes_DO_treat5 <-recode(modeldata$Vignettes_DO_treat5, " 1 = 5; else=0")
modeldata$Vignettes_DO_treat6 <-recode(modeldata$Vignettes_DO_treat6, " 1 = 6; else=0")

modeldata$treat<-modeldata$Vignettes_DO_treat1 +  modeldata$Vignettes_DO_treat2 + modeldata$Vignettes_DO_treat3 + modeldata$Vignettes_DO_treat4 + modeldata$Vignettes_DO_treat5 + modeldata$Vignettes_DO_treat6

table(modeldata$treat)

modeldata$treat<-recode(modeldata$treat, " 0 = NA")

##sexual harassment vignettes (treat 1 = AMP, treat 2 = GBP, treat 3 = Q-GBP)

#subset the data for the issue area of sexual harassment 

harass<-subset(modeldata, treat == 1 | treat == 2 | treat == 3)

#Create indicies for three treatment groups 

#for substantive 
factor1<-omega(harass[,c(36, 37, 39)], nfactors=1) #alpha = 0.89
harass$SubLeg<-factor1$scores[,1]
harass$SubLegStand<-standardize(harass$SubLeg)

#for procedural legitimacy 
factor1<-omega(harass[,c(41, 61, 43)], nfactors=1) #alpha = 0.81
harass$ProLeg<-factor1$scores[,1]
harass$ProLegStand<-standardize(harass$ProLeg)


##subset the animal mistreatment vignettes 

animal<-subset(modeldata, treat == 4 | treat == 5 | treat == 6)

#for substantive legitimacy
factor1<-omega(animal[,c(36,38, 40)], nfactors=1) #alpha = 0.79
animal $SubLeg<-factor1$scores[,1]
animal $SubLegStand<-standardize(animal $SubLeg)

#for procedural legit 
factor1<-omega(animal[,c(41, 61, 43)], nfactors=1) #alpha = 0.83
animal $ProLeg<-factor1$scores[,1]
animal $ProLegStand<-standardize(animal $ProLeg)


animal <- animal[!is.na(animal $SubLegStand),] #remove NA values from DV

animal <- animal[!is.na(animal $ProLegStand),] #remove NA values from DV

#create and export model dataset 

MexAgg<-rbind(harass[, c(62, 64, 66, 7, 8)], animal[, c(62, 64, 66, 7, 8)]) 
MexAgg$Country <-rep("Mexico", nrow(MexAgg))
write.csv(MexAgg, "MexAgg.csv")



